package com.atguigu.ware.wares.service.impl;

import com.atguigu.common.exception.enumtion.WmsPurchaseDatailStatusEnum;
import com.atguigu.common.exception.enumtion.WmsPurchaseStatusEnum;
import com.atguigu.ware.wares.entity.WmsPurchaseDetailEntity;
import com.atguigu.ware.wares.service.WmsPurchaseDetailService;
import com.atguigu.ware.wares.service.WmsWareSkuService;
import com.atguigu.ware.wares.vo.reqVo.WarePurchaseDoneReqVo;
import com.atguigu.ware.wares.vo.reqVo.WarePurchaseMergeReqVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.ware.wares.dao.WmsPurchaseDao;
import com.atguigu.ware.wares.entity.WmsPurchaseEntity;
import com.atguigu.ware.wares.service.WmsPurchaseService;
import org.springframework.transaction.annotation.Transactional;


@Service("wmsPurchaseService")
public class WmsPurchaseServiceImpl extends ServiceImpl<WmsPurchaseDao, WmsPurchaseEntity> implements WmsPurchaseService {


    @Autowired
    private WmsPurchaseDetailService purchaseDetailService;

    @Autowired
    private WmsWareSkuService wmsWareSkuService;



    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<WmsPurchaseEntity> page = this.page(
                new Query<WmsPurchaseEntity>().getPage(params),
                new QueryWrapper<WmsPurchaseEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 查询状态为新建和已分配的采购单
     *
     * @param params
     * @return
     */
    public PageUtils unreceiveList(Map<String, Object> params) {
        IPage<WmsPurchaseEntity> page = this.page(
                new Query<WmsPurchaseEntity>().getPage(params),
                new QueryWrapper<WmsPurchaseEntity>().eq("status", WmsPurchaseStatusEnum.CREATED.getCode()).or().eq("status", WmsPurchaseStatusEnum.ASSIGNED.getCode())
        );

        return new PageUtils(page);
    }

    /**
     * 合并采购需求
     *
     * @param purchaseMergeReqVo
     */
    @Transactional
    public void merge(WarePurchaseMergeReqVo purchaseMergeReqVo) {
        if (purchaseMergeReqVo.getPurchaseId() == null) {
            //需要新建采购单来合并
            WmsPurchaseEntity en = new WmsPurchaseEntity();
            en.setPriority(0);
            en.setCreateTime(new Date());
            en.setUpdateTime(new Date());
            en.setStatus(0);
            this.save(en);
            purchaseMergeReqVo.setPurchaseId(en.getId());
        }

        //拿到采购需求整合到采购单中
        List<WmsPurchaseDetailEntity> purchaseDetailEntities = purchaseMergeReqVo.getItems().stream().map((m) -> {
            WmsPurchaseDetailEntity ens = new WmsPurchaseDetailEntity();
            ens.setId(m);
            ens.setPurchaseId(purchaseMergeReqVo.getPurchaseId());
            ens.setStatus(WmsPurchaseDatailStatusEnum.ASSIGNED.getCode());
            return ens;

        }).collect(Collectors.toList());

        //批量修改
        purchaseDetailService.updateBatchById(purchaseDetailEntities);

    }

    /**
     * 领取采购单
     * @param purchaseIds
     */
    @Transactional
    public void received(List<Long> purchaseIds) {
        //我们需要查询符合条件的采购单
        List<WmsPurchaseEntity> wmsPurchaseEntities = this.baseMapper.selectBatchIds(purchaseIds);
        List<WmsPurchaseEntity> purchaseEntities = wmsPurchaseEntities.stream().filter((f) -> {
            if (f.getStatus() == WmsPurchaseStatusEnum.CREATED.getCode() ||
                    f.getStatus() == WmsPurchaseStatusEnum.ASSIGNED.getCode()) {
                return true;
            }
            return false;
        }).map((m2) -> {
            m2.setStatus(2);
            m2.setUpdateTime(new Date());
            return m2;
        }).collect(Collectors.toList());

        //批量更新
        this.updateBatchById(purchaseEntities);

        //查询对应的采购需求单
          //拿到符合条件的id
        List<Long> ids = purchaseEntities.stream().map(m -> m.getId()).collect(Collectors.toList());
        List<WmsPurchaseDetailEntity> wmsPurchaseDetailEntityList = purchaseDetailService.getBaseMapper().selectList(new QueryWrapper<WmsPurchaseDetailEntity>().in("purchase_id", ids));

        List<WmsPurchaseDetailEntity> wmsPurchaseDetailEntities = wmsPurchaseDetailEntityList.stream().map((m3) -> {
            WmsPurchaseDetailEntity ens = new WmsPurchaseDetailEntity();
            BeanUtils.copyProperties(m3, ens);
            ens.setStatus(2);
            return ens;
        }).collect(Collectors.toList());
        //批量修改采购需求
        purchaseDetailService.updateBatchById(wmsPurchaseDetailEntities);


    }

    /**
     * 完成采购
     * @param purchaseMergeReqVo
     */
    @Transactional
    public void done(WarePurchaseDoneReqVo purchaseMergeReqVo) {

         //完成采购需求
        List<WmsPurchaseDetailEntity> wmsPurchaseDetailEntities = purchaseMergeReqVo.getItems().stream().map((m -> {
            WmsPurchaseDetailEntity en = new WmsPurchaseDetailEntity();
            BeanUtils.copyProperties(m, en);
            en.setId(m.getItemId());
            return en;
        })).collect(Collectors.toList());
        //批量更新我们的采购需求表
        purchaseDetailService.updateBatchById(wmsPurchaseDetailEntities);

        //根据我们采购需求单去更新采购单表和商品库存库存表
        Boolean flag=true;
        for(WmsPurchaseDetailEntity entity:wmsPurchaseDetailEntities){
            if(entity.getStatus()==WmsPurchaseDatailStatusEnum.HASERROR.getCode()){
                flag=false;
            }else {

                //采购成功的需要入库
                //根据id查询对应的需求表数据
                WmsPurchaseDetailEntity wmsPurchaseDetailEntity = purchaseDetailService.getBaseMapper().selectById(entity.getId());
                wmsWareSkuService.addWareSkus(wmsPurchaseDetailEntity.getSkuId(),wmsPurchaseDetailEntity.getWareId(),wmsPurchaseDetailEntity.getSkuNum());
            }
        }



        //调用接口完成采购单修改
        WmsPurchaseEntity entity=new WmsPurchaseEntity();
        entity.setId(purchaseMergeReqVo.getId());
        entity.setStatus(flag?WmsPurchaseStatusEnum.FINISH.getCode():WmsPurchaseStatusEnum.HASERROR.getCode());
        entity.setUpdateTime(new Date());

        this.updateById(entity);

    }


}